package com.aidex.biz.utils.hengchang;

import com.aidex.biz.domain.Customer;
import com.aidex.biz.service.ZifangPushService;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.http.HttpUtils;
import com.aidex.common.utils.sign.Md5Utils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class HengChangServerImpl implements HengChangServer {
    private final static String HOST = "https://cloud-xrz-service.huizhongxt.com";
    /**
     * 商户号
     */
    private final static String channelNum = "QDBH_20250206_006215";
    private final static String channelId = "127.0.0.1";

    private final static String channelNickname = "江西华财(API)";


    private static String HengChangPubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0Ft+ytal+v2t72Qpzj70+ZQFO" +
            "+wKwT92W1RSkSIz9+AD42GSoLXWU7CcEWL7eRP0CRRiCR8qtA/iFuPijX92cB1TZy7mIkw0nKO9OLa" +
            "+h9bmqCq70HdMuOnAmhUcWYJtRSrgXjD+WyAIIgU7xNqlpBI+bloN+qVCxw9M90fs4NQIDAQAB";

    private static String JxPubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC69AaXls2j4v0/LFlY80Lk4ditd4tLDFgz/h7u" +
            "+8XlnhRrvnnD95VDTuS+IfSWYuzz9yJQWwWfV446wtUl4h1Wl8bR+7fEKVO7wHHDl5wQfE6Y5LiiZspnhu5C0tqiVg5ORbBExg4" +
            "/xbTWhN9QYj91IgEio0Bp6o6gBeWTUtOPRQIDAQAB";
    private static String JxPivKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALr0BpeWzaPi" +
            "/T8sWVjzQuTh2K13i0sMWDP+Hu77xeWeFGu" +
            "+ecP3lUNO5L4h9JZi7PP3IlBbBZ9XjjrC1SXiHVaXxtH7t8QpU7vAccOXnBB8TpjkuKJmymeG7kLS2qJWDk5FsETGDj" +
            "/FtNaE31BiP3UiASKjQGnqjqAF5ZNS049FAgMBAAECgYAtOoIpxEsFvhGEagdHlLp3Qwg3lzc0tczU70KukGylC2RnWJJ+zXPn77s5IF" +
            "/azh4DO6OqiwrGLNl079O/MX" +
            "/lHEA05tm9hjcGVPTFraFF9s0XKQra1cwtplwzx8oDq80ba3Knn7da2QNFVyB6svmXkTcw03hCpALfqY3Ihq2mNQJBAN21Rq" +
            "/pYTh6RrmGn6GrW+fydLwXj80ndhMc/FODadaiY8gITtABzEwuldYc9e2muVRxZihTJPCQpHzBPNlquJsCQQDX3pmZ0JvPFB" +
            "/CMrT24KV3Ilqqo6AxYSPBc49RR08jS1AffVEysdAJIMBL" +
            "+pgDmZSilXevqMqEtopTsL61HaWfAkB5BKMyEGUGDBCg4nAuMjg3kqvPo9jVtg2If1DoURork3Z90LMcbW7TwrX9Ks++yjvtcPy" +
            "/VCscuQdgJaaOUuezAkBovPKd9N+ES+lU2jF/j2P5N8vx+04gZ1dn51/kOcsl" +
            "/sazYKRjCm873o4uzzmulXfOouLdcfJBDihzjtAZJHdVAkEA0zAA6zfYUPLBKUzV4UKM/NwFkCYkWx2h3Zk1" +
            "/pZCxSrPqD4oDLyOFrLb5Oyab4G+RNIfAru+cD4TBU9RblXdKg==";

    @Autowired
    private ZifangPushService zifangPushService;

    @Override
    public JSONObject synChannelCustomerInfo(Customer customer) {
        // 准备请求参数 对象
        JSONObject params = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            params.put("channelNum", channelNum);
            String key = CryptDESUtil.createDesKey();
            // 通过RSA的publicKey对DES随机钥匙加密
            String requestKey = ClientRSAUtil.rsaEncryptByPubKey(HengChangPubKey, key);
            params.put("requestKey", requestKey);
            // 用DES算法加密request报文
            JSONObject cusJson = new JSONObject();
            String channelOrderNum = customer.getZiFangPushId();
            cusJson.put("channelOrderNum", channelOrderNum);
            cusJson.put("name", customer.getCusName());
            cusJson.put("phoneNum", customer.getPhone());
            cusJson.put("province", customer.getCityName());
            cusJson.put("city", customer.getCityName());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            cusJson.put("applyTime", simpleDateFormat.format(new Date()));
            cusJson.put("pushTime", simpleDateFormat.format(new Date()));
            cusJson.put("licenseNo", customer.getCarNo());
            cusJson.put("brand", customer.getCarModel());
            cusJson.put("carType", customer.getCarType()); // 车辆型号
            cusJson.put("chassisNo", customer.getChassisNo()); // 车架号
            cusJson.put("firstLicenseTime", customer.getFuckCarNoTime());
            cusJson.put("thisLicenseTime", customer.getFuckCarNoTime());
            cusJson.put("mileage", customer.getMileage()); // 公里数
            //cusJson.put("isBelong",customer.getIsBelong()); // 是否在本人名下
            // cusJson.put("isLoan",customer.getIsLoan()); // 是否有贷款
            // cusJson.put("loanStatus",customer.getLoanStatus());// 车辆贷款情况
            // cusJson.put("mortgageStatus",customer.getMortgageStatus());// 抵押情况
            cusJson.put("realName", customer.getRealName()); // 真是姓名
            cusJson.put("cardType", 1); //
            cusJson.put("cardNum", customer.getIdCard());
            cusJson.put("bankName", customer.getBankName()); // 银行名称
            cusJson.put("bankNum", customer.getBankNum()); // 银行卡号
            cusJson.put("residenceAddress", customer.getAddress());// 户籍地址
            if (StringUtils.isNotNull(customer.getIdCardStartTime())) {
                cusJson.put("idCardStartTime", simpleDateFormat.format(customer.getIdCardStartTime())); // 身份证有效期开始时间
            }
            if (StringUtils.isNotNull(customer.getIdCardEndTime())) {
                cusJson.put("idCardEndTime", simpleDateFormat.format(customer.getIdCardEndTime())); // 身份证有效期
            }
            cusJson.put("applyAmount", customer.getAmount());
//             cusJson.put("applyPeriods",customer.getPeriods());
            cusJson.put("clueSource", 1);
            cusJson.put("customerSource", 1);
            String requestData = CryptDESUtil.desEntrypt(key, cusJson.toString());
            params.put("requestData", requestData);
            String res = HttpUtils.doPost(HOST + "/synChannelCustomerInfo", params);
            jsonObject.put("data", res);
            jsonObject.put("code", 200);
            jsonObject.put("channelOrderNum", channelOrderNum);
        } catch (Exception exception) {
            jsonObject.put("msg", exception.getMessage());
            jsonObject.put("code", 500);
        }
        return jsonObject;
    }

    /**
     * 查证
     *
     * @param channelOrderNum
     * @return
     */

    @Override
    public JSONObject thirdQueryStatus(String channelOrderNum) {
        // 准备请求参数 对象
        JSONObject params = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {

            String res = HttpUtils.doPost(HOST, params);
            jsonObject.put("msg", res);
            jsonObject.put("code", 200);
        } catch (Exception exception) {
            jsonObject.put("msg", exception.getMessage());
            jsonObject.put("code", 500);
        }
        return jsonObject;
    }


    /**
     * 撞库
     *
     * @param customer
     * @return
     */
    @Override
    public JSONObject synCustomerInfoPreApprovalRushData(Customer customer) {
        // 准备请求参数 对象
        JSONObject params = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            params.put("phoneMd5", Md5Utils.hash(customer.getPhone()));
            params.put("channelId", channelNum);
            params.put("channelNickname", channelNickname);
            String res = HttpUtils.doPost(HOST + "/synCustomerInfoPreApprovalRushData", params);
            jsonObject = JSONObject.parseObject(res);
            if (200 == jsonObject.getInteger("code")) {
                if ("1".equals(jsonObject.getJSONObject("body").getString("isPass"))) {
                    jsonObject.put("code", 200);
                } else {
                    jsonObject.put("code", 500);
                }
            } else {
                jsonObject.put("code", 500);
            }

        } catch (Exception exception) {
            jsonObject.put("msg", exception.getMessage());
            jsonObject.put("code", 500);
        }
        return jsonObject;
    }

    @Override
    public JSONObject synCustomerInfoCallBack(String channelNum, String requsetData) {
        JSONObject res = new JSONObject();
        try {
            if (this.channelNum.equals(channelNum)) {
                String data = ClientRSAUtil.rsaDecryptByPriKey(JxPivKey, requsetData);
                res = JSONObject.parseObject(data);
            } else {
                res.put("code", 500);
                res.put("desc", "商户编号错误");
            }
        } catch (Exception exception) {
            res.put("code", 500);
            res.put("desc", exception.getMessage());
        }
        return res;
    }

    public static void main(String[] args) throws Exception {
        String string = "123232";
        String jiamiData = ClientRSAUtil.rsaEncryptByPubKey(JxPubKey, string);
        System.out.println("加密：" + jiamiData);
        String jiemiData = ClientRSAUtil.rsaDecryptByPriKey(JxPivKey, jiamiData);
        System.out.println("解密：" + jiemiData);
    }


}
